package com.dragon.backstage.convert;

import com.dragon.backstage.domain.param.role.RoleAuthUserParam;
import com.dragon.backstage.domain.param.user.UserParam;
import com.dragon.backstage.domain.vo.user.UserAuthRoleVo;
import com.dragon.backstage.domain.vo.user.UserDetailVo;
import com.dragon.backstage.domain.vo.user.UserVo;
import com.dragon.interfaces.constant.UserConstant;
import com.dragon.interfaces.domain.request.user.UserRequest;
import com.dragon.interfaces.entity.Role;
import com.dragon.interfaces.entity.User;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * 用户模块类型转换
 *
 * @author dragon
 * @version 1.0
 * @date 2024/8/7 17:59
 */
public class UserConvert {

    public static UserRequest toUserRequest(RoleAuthUserParam roleAuthUserParam) {
        return UserRequest
            .builder()
            .pageNum(roleAuthUserParam.getPageNum())
            .pageSize(roleAuthUserParam.getPageSize())
            .roleId(roleAuthUserParam.getRoleId())
            .account(roleAuthUserParam.getAccount())
            .phoneNumber(roleAuthUserParam.getPhoneNumber())
            .build();
    }

    public static UserVo toUserVo(User user) {
        return UserVo
            .builder()
            .isSuperAdmin(UserConstant.SUPER_ADMIN_ACCOUNT.equals(user.getAccount()))
            .userId(user.getUserId())
            .account(user.getAccount())
            .nickName(user.getNickName())
            .email(user.getEmail())
            .phoneNumber(user.getPhoneNumber())
            .status(user.getStatus())
            .createTime(user.getCreateTime())
            .build();
    }

    public static UserRequest toUserRequest(UserParam userParam) {
        return UserRequest
            .builder()
            .pageNum(userParam.getPageNum())
            .pageSize(userParam.getPageSize())
            .roleId(userParam.getRoleId())
            .account(userParam.getAccount())
            .phoneNumber(userParam.getPhoneNumber())
            .status(userParam.getStatus())
            .beginTime(userParam.getBeginTime())
            .endTime(userParam.getEndTime())
            .excludeAccounts(userParam.getExcludeAccounts())
            .build();
    }

    public static UserDetailVo toUserDetailVo(User user) {
        return UserDetailVo
            .builder()
            .userId(user.getUserId())
            .account(user.getAccount())
            .nickName(user.getNickName())
            .userType(user.getUserType())
            .email(user.getEmail())
            .phoneNumber(user.getPhoneNumber())
            .sex(user.getSex())
            .avatar(user.getAvatar())
            .status(user.getStatus())
            .loginIp(user.getLoginIp())
            .loginTime(user.getLoginTime())
            .remark(user.getRemark())
            .createTime(user.getCreateTime())
            .createBy(user.getCreateBy())
            .updateTime(user.getUpdateTime())
            .updateBy(user.getUpdateBy())
            .build();
    }

    public static UserAuthRoleVo toUserAuthRoleVo(Role role, List<Long> roleIds) {
        return UserAuthRoleVo
            .builder()
            .roleId(role.getRoleId())
            .roleName(role.getRoleName())
            .roleKey(role.getRoleKey())
            .roleSort(role.getRoleSort())
            .status(role.getStatus())
            .createTime(role.getCreateTime())
            .createBy(role.getCreateBy())
            .updateTime(role.getUpdateTime())
            .updateBy(role.getUpdateBy())
            .isOwn(roleIds.contains(role.getRoleId()))
            .build();
    }

}
